package cn.aj.pet.service.impl;

import cn.aj.basic.exception.BussinessException;
import cn.aj.basic.utils.AjaxResultUtil;
import cn.aj.basic.utils.BaiduAuditUtils;
import cn.aj.pet.domain.Pet;
import cn.aj.pet.domain.PetDetail;
import cn.aj.pet.domain.PetOnlineAuditLog;
import cn.aj.pet.mapper.PetDetailMapper;
import cn.aj.pet.mapper.PetMapper;
import cn.aj.pet.mapper.PetOnlineAuditLogMapper;
import cn.aj.pet.service.IPetService;
import cn.aj.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author aj
 * @since 2022-12-03
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    /**
    * @Title: add
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2022/12/3 0003 22:11
    * @Parameters: [pet]
    * @Return void
     * 添加富文本
    */
    @Override
    @Transactional
    public void add(Pet pet) {
        super.add(pet);
        // 添加富文本
        PetDetail detail = pet.getDetail();
        detail.setPetId(pet.getId());
        petDetailMapper.add(detail);

    }

    /**
    * @Title: petOnsale
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2022/12/3 0003 22:11
    * @Parameters: [ids]
    * @Return cn.aj.basic.utils.AjaxResultUtil
     * 宠物上架
    */
    @Override
    @Transactional
    public void petOnsale(List<Long> ids) {
        // 判断前端的传过来的参数
        if (CollectionUtils.isEmpty(ids)) {
            throw new BussinessException("您的参数为空!");
        }
        // 先排除已经上架的,剩下就是未上架的
        List<Pet> petList = petMapper.getByIdsAndStatePetOnsale(ids);
        if (CollectionUtils.isEmpty(petList)) { // 判断过滤非空
            return;
        }
        // 百度ai审核
        List<PetOnlineAuditLog> auditLogs = new ArrayList<>();
        List<Long> petIds = new ArrayList<>();
        petList.forEach(pet -> {
            StringBuilder sb = new StringBuilder();
            // 审核名称
            if (!BaiduAuditUtils.TextCensor(pet.getName())) {
                sb.append("宠物名称审核不通过，名称为:" + pet.getName()).append(",");
            }
            // 审核图片 我们的resources是使用, 隔开的 我们需要一个一个去审核
            String resources = pet.getResources();
            Arrays.stream(resources.split(",")).forEach(img -> {
                if (!BaiduAuditUtils.ImgCensor(img)) {
                    sb.append("宠物图片审核不通过,名称为:" + pet.getName());
                   //  throw new BussinessException("当前上传的图片违规,上架失败!");
                }
            });
            if (sb.length() > 0) {
                // 添加到日志表
                PetOnlineAuditLog auditLog = new PetOnlineAuditLog();
                auditLog.setAuditId(-1L);
                auditLog.setAuditTime(new Date());
                auditLog.setNote(sb.toString());
                auditLog.setState(0);
                auditLog.setPetId(pet.getId());
                auditLogs.add(auditLog);
                return;
            }
            // 修改状态
            petIds.add(pet.getId());
        });
        //	4.通过 修改状态为上架 --- 上架时间 下架时间
        if (!CollectionUtils.isEmpty(petIds)) {
            petMapper.batchUpdateOn(petIds);
            for (Pet pet : petList) { // 添加上架的日志
                PetOnlineAuditLog OnauditLog = new PetOnlineAuditLog();
                OnauditLog.setAuditId(-1L);
                OnauditLog.setAuditTime(new Date());
                OnauditLog.setState(1);
                OnauditLog.setPetId(pet.getId());
                OnauditLog.setNote("宠物为" + ":" + pet.getName() + "上架成功");
                petOnlineAuditLogMapper.add(OnauditLog);
            }
        }
        if (!CollectionUtils.isEmpty(auditLogs)) {
            //	5.未通过 不修改状态添加日志
            petOnlineAuditLogMapper.batchAdd(auditLogs);
        }
    }

    /**
     * @Title: petOffsale
     * @Description: TODO
     * @Author: Mr.GT
     * @Version: 1.0
     * @Date:  2022/12/3 0003 23:05
     * @Parameters: [ids]
     * @Return void
     * 宠物下架
     * 直接下架
     */
    @Override
    @Transactional
    public void petOffsale(List<Long> ids) {
        // 先查询已经上架了的,剩下就是下架的
        List<Pet> petList = petMapper.getByIdsAndStatePetOffsale(ids);
        petMapper.batchUpdateOff(ids);
        for (Pet pet : petList) { // 添加下架的日志
            PetOnlineAuditLog OffauditLog = new PetOnlineAuditLog();
            OffauditLog.setAuditId(-1L);
            OffauditLog.setAuditTime(new Date());
            OffauditLog.setState(0);
            OffauditLog.setNote("宠物为:" + ":" + pet.getName() + "下架成功");
            OffauditLog.setPetId(pet.getId());
            petOnlineAuditLogMapper.add(OffauditLog);
         }
     }

    /**
    * @Title: update
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2022/12/8 0008 14:00
    * @Parameters: [pet]
    * @Return void
     * 富文本修改
    */
    @Override
    public void update(Pet pet) {
        super.update(pet);
        petDetailMapper.update(pet.getDetail());
    }
}
